﻿'*******************************************************************
' Program: Data Center Visio Rack Tool
' Author: Albert E Edlund
' Date:
'
' Purpose:
'
'*******************************************************************

Imports Microsoft.Practices.EnterpriseLibrary.Common
Imports Microsoft.Practices.EnterpriseLibrary.Data
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling
Imports Microsoft.Practices.EnterpriseLibrary.ExceptionHandling.Logging

Imports System.Runtime.InteropServices
Imports Microsoft.Office.Interop
Imports Microsoft.Office.Interop.Visio


Imports System
Imports System.ComponentModel
Imports System.Collections.Generic
Imports System.Collections.ObjectModel


Imports System.Data
Imports System.IO
Imports System.Xml
Imports System.Collections


Imports DC_Rack.Objects
Imports DC_Rack.Objects.clsDC_RackData





Public Class clsDgMasters


#Region " Properties and declarations "

    Public m_db As Database
    Public m_ds As DataSet
    Public m_dt As DataTable

    Public m_dc As dc_DGMaster
    Public m_oc As Collection(Of dc_DGMaster)

    Private strUserId As String

    Public m_deleteCommand As DbCommand
    Public m_insertCommand As DbCommand
    Public m_updateCommand As DbCommand


    Public Const readAll As String = "selectTblDgMasters"

    ' who am I
    Private myEnum As DC_RackTables = DC_RackTables.tblDGMasters

    Private m_clsVisio As DC_Rack.Support.clsVisioSupport
    Property _clsVisio() As DC_Rack.Support.clsVisioSupport
        Get
            Return Me.m_clsVisio
        End Get
        Set(ByVal value As DC_Rack.Support.clsVisioSupport)
            Me.m_clsVisio = value
        End Set
    End Property


    '********* visio snippet vb.net visXXXX ******************
    Private _drawingApplication As Microsoft.Office.Interop.Visio.Application
    Public Property drawingApplication() As Microsoft.Office.Interop.Visio.Application
        Get
            Return Me._drawingApplication
        End Get
        Set(ByVal value As Microsoft.Office.Interop.Visio.Application)
            If (value Is Nothing) Then
                Throw New System.ArgumentNullException("ApplicationValueIsNull", _
                    "drawingApplication is Null.")
            End If
            Me._drawingApplication = value
        End Set
    End Property

    '********* visio snippet vb.net visXXXX ******************
    Private _drawingDocument As Microsoft.Office.Interop.Visio.Document
    Public Property drawingDocument() As Microsoft.Office.Interop.Visio.Document
        Get
            Return Me._drawingDocument
        End Get
        Set(ByVal value As Microsoft.Office.Interop.Visio.Document)
            If (value Is Nothing) Then
                Throw New System.ArgumentNullException("DocumentValueIsNull", _
                    "drawingDocument is Null.")
            End If
            Me._drawingDocument = value
        End Set
    End Property


    '********* visio snippet vb.net visXXXX ******************
    Private _drawingWindow As Microsoft.Office.Interop.Visio.Window
    Public Property drawingWindow() As Microsoft.Office.Interop.Visio.Window
        Get
            Return Me._drawingWindow
        End Get
        Set(ByVal value As Microsoft.Office.Interop.Visio.Window)
            If (value Is Nothing) Then
                Throw New System.ArgumentNullException("WindowValueIsNull", _
                    "drawingWindow is Null.")
            End If
            Me._drawingWindow = value
        End Set
    End Property

    '********* visio snippet vb.net visXXXX ******************
    Private _drawingPage As Microsoft.Office.Interop.Visio.Page
    Public Property drawingPage() As Microsoft.Office.Interop.Visio.Page
        Get
            Return Me._drawingPage
        End Get
        Set(ByVal value As Microsoft.Office.Interop.Visio.Page)
            If (value Is Nothing) Then
                Throw New System.ArgumentNullException("PageValueIsNull", _
                    "drawingPage is Null.")
            End If
            Me._drawingPage = value
        End Set
    End Property


    Private m_blnLoadStencil As Boolean
    Property blnLoadStencil As Boolean
        Get
            Return m_blnLoadStencil
        End Get
        Set(ByVal value As Boolean)
            m_blnLoadStencil = value
        End Set
    End Property

    Private m_StencilPath As String
    Property _StencilPath() As String
        Get
            Return Me.m_StencilPath
        End Get
        Set(ByVal value As String)
            Me.m_StencilPath = value
        End Set
    End Property

    Private m_dictStencils As Dictionary(Of Integer, String)
    Property _dictStencils() As Dictionary(Of Integer, String)
        Get
            If m_dictStencils Is Nothing Then
                m_dictStencils = New Dictionary(Of Integer, String)
            End If
            _dictStencils = m_dictStencils
        End Get
        Set(ByVal value As Dictionary(Of Integer, String))
            m_dictStencils = value
        End Set
    End Property

    Private m_dictMasters As Dictionary(Of Integer, String)
    Property _dictMasters() As Dictionary(Of Integer, String)
        Get
            If m_dictMasters Is Nothing Then
                m_dictMasters = New Dictionary(Of Integer, String)
            End If
            _dictMasters = m_dictMasters
        End Get
        Set(ByVal value As Dictionary(Of Integer, String))
            m_dictMasters = value
        End Set
    End Property





#End Region

#Region " Move/Transform Data "


    '
    ' create columns
    '
    Public Sub CreateTableColumns(ByRef tempTable As DataTable)

        Dim newDC As dc_DGMaster = New dc_DGMaster(strUserId)

        Try

            Dim coldgmTable As New System.Data.DataColumn
            coldgmTable.ColumnName = "dgmTable"
            coldgmTable.DataType = GetType(String)
            coldgmTable.DefaultValue = m_dc.dgmTable
            tempTable.Columns.Add(coldgmTable)

            Dim coldgmSurrogateMaster As New System.Data.DataColumn
            coldgmSurrogateMaster.ColumnName = "dgmSurrogateMaster"
            coldgmSurrogateMaster.DataType = GetType(String)
            coldgmSurrogateMaster.DefaultValue = m_dc.dgmSurrogateMaster
            tempTable.Columns.Add(coldgmSurrogateMaster)

            Dim coldgmSurrogateStencil As New System.Data.DataColumn
            coldgmSurrogateStencil.ColumnName = "dgmSurrogateStencil"
            coldgmSurrogateStencil.DataType = GetType(String)
            coldgmSurrogateStencil.DefaultValue = m_dc.dgmSurrogateStencil
            tempTable.Columns.Add(coldgmSurrogateStencil)

            Dim coldgmSurrogatePrefix As New System.Data.DataColumn
            coldgmSurrogatePrefix.ColumnName = "dgmSurrogatePrefix"
            coldgmSurrogatePrefix.DataType = GetType(String)
            coldgmSurrogatePrefix.DefaultValue = m_dc.dgmSurrogatePrefix
            tempTable.Columns.Add(coldgmSurrogatePrefix)

            Dim coldgmMaster As New System.Data.DataColumn
            coldgmMaster.ColumnName = "dgmMaster"
            coldgmMaster.DataType = GetType(String)
            coldgmMaster.DefaultValue = m_dc.dgmMaster
            tempTable.Columns.Add(coldgmMaster)

            Dim coldgmStencil As New System.Data.DataColumn
            coldgmStencil.ColumnName = "dgmStencil"
            coldgmStencil.DataType = GetType(String)
            coldgmStencil.DefaultValue = m_dc.dgmStencil
            tempTable.Columns.Add(coldgmStencil)

            Dim coldgmFontValue As New System.Data.DataColumn
            coldgmFontValue.ColumnName = "dgmFontValue"
            coldgmFontValue.DataType = GetType(Int16)
            coldgmFontValue.DefaultValue = m_dc.dgmFontValue
            tempTable.Columns.Add(coldgmFontValue)

            Dim coldgmFontLabel As New System.Data.DataColumn
            coldgmFontLabel.ColumnName = "dgmFontLabel"
            coldgmFontLabel.DataType = GetType(Int16)
            coldgmFontLabel.DefaultValue = m_dc.dgmFontLabel
            tempTable.Columns.Add(coldgmFontLabel)

            Dim coldgmCalloutWidth As New System.Data.DataColumn
            coldgmCalloutWidth.ColumnName = "dgmCalloutWidth"
            coldgmCalloutWidth.DataType = GetType(Double)
            coldgmCalloutWidth.DefaultValue = m_dc.dgmCalloutWidth
            tempTable.Columns.Add(coldgmCalloutWidth)

            Dim coldgmComments As New System.Data.DataColumn
            coldgmComments.ColumnName = "dgmComments"
            coldgmComments.DataType = GetType(String)
            coldgmComments.DefaultValue = m_dc.dgmComments
            tempTable.Columns.Add(coldgmComments)

            Dim coldgmLastUpdate As New System.Data.DataColumn
            coldgmLastUpdate.ColumnName = "dgmLastUpdate"
            coldgmLastUpdate.DataType = GetType(Date)
            coldgmLastUpdate.DefaultValue = m_dc.dgmLastUpdate
            tempTable.Columns.Add(coldgmLastUpdate)

            Dim coldgmLastEdit As New System.Data.DataColumn
            coldgmLastEdit.ColumnName = "dgmLastEdit"
            coldgmLastEdit.DataType = GetType(String)
            coldgmLastEdit.DefaultValue = m_dc.dgmLastEdit
            tempTable.Columns.Add(coldgmLastEdit)

            newDC = Nothing

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

        If newDC IsNot Nothing Then
            newDC = Nothing
        End If


    End Sub


    '
    ' use for writing records
    '
    Public Sub movePropertiesToDataRow _
                (ByRef tempRow As DataRow, _
                 ByVal tempDC As dc_DGMaster)

        Try
            With tempDC
                tempRow.SetField("dgmTable", .dgmTable)
                tempRow.SetField("dgmSurrogateMaster", .dgmSurrogateMaster)
                tempRow.SetField("dgmSurrogateStencil", .dgmSurrogateStencil)
                tempRow.SetField("dgmSurrogatePrefix", .dgmSurrogatePrefix)
                tempRow.SetField("dgmMaster", .dgmMaster)
                tempRow.SetField("dgmStencil", .dgmStencil)
                tempRow.SetField("dgmFontValue", .dgmFontValue)
                tempRow.SetField("dgmFontLabel", .dgmFontLabel)
                tempRow.SetField("dgmCalloutWidth", .dgmCalloutWidth)
                tempRow.SetField("dgmComments", .dgmComments)
                tempRow.SetField("dgmLastUpdate", .dgmLastUpdate)
                tempRow.SetField("dgmLastEdit", strUserId)
            End With


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    ' for reading records
    '
    Public Sub moveDataRowToProperties _
                (ByVal tempRow As DataRow)

        Try
            With m_dc
                .initProperties()
                .dgmTable = tempRow.Item("dgmTable")
                .dgmSurrogateMaster = tempRow.Item("dgmSurrogateMaster")
                .dgmSurrogateStencil = tempRow.Item("dgmSurrogateStencil")
                .dgmSurrogatePrefix = tempRow.Item("dgmSurrogatePrefix")
                .dgmMaster = tempRow.Item("dgmMaster")
                .dgmStencil = tempRow.Item("dgmStencil")
                .dgmFontValue = tempRow.Item("dgmFontValue")
                .dgmFontLabel = tempRow.Item("dgmFontLabel")
                .dgmCalloutWidth = tempRow.Item("dgmCalloutWidth")
                .dgmComments = tempRow.Item("dgmComments")
                .dgmLastUpdate = tempRow.Item("dgmLastUpdate")
                .dgmLastEdit = tempRow.Item("dgmLastEdit")
            End With


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub


    '
    ' for reading records
    '
    Public Sub moveDataRowToProperties _
                (ByVal tempRow As DataRow, _
                 ByRef tempDC As dc_DGMaster)


        Dim dcTemp As dc_DGMaster = New dc_DGMaster

        Try
            With tempDC
                '.initProperties()
                .dgmTable = tempRow.Item("dgmTable")
                .dgmSurrogateMaster = tempRow.Item("dgmSurrogateMaster")
                .dgmSurrogateStencil = tempRow.Item("dgmSurrogateStencil")
                .dgmSurrogatePrefix = tempRow.Item("dgmSurrogatePrefix")
                .dgmMaster = tempRow.Item("dgmMaster")
                .dgmStencil = tempRow.Item("dgmStencil")
                .dgmFontValue = tempRow.Item("dgmFontValue")
                .dgmFontLabel = tempRow.Item("dgmFontLabel")
                .dgmCalloutWidth = tempRow.Item("dgmCalloutWidth")
                .dgmComments = tempRow.Item("dgmComments")
                .dgmLastUpdate = tempRow.Item("dgmLastUpdate")
                .dgmLastEdit = tempRow.Item("dgmLastEdit")
            End With


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

        Dim strTest As String = ""

    End Sub

    '
    '
    '
    Public Sub moveDataTableToObservableCollection _
        (ByVal dtTemp As DataTable)

        Try

            If 0 < dtTemp.Rows.Count Then

                If m_oc IsNot Nothing Then
                    m_oc = Nothing
                    m_oc = New Collection(Of dc_DGMaster)
                Else
                    m_oc = New Collection(Of dc_DGMaster)
                End If

                Dim drTemp As DataRow
                Dim dcTemp As dc_DGMaster
                For Each drTemp In dtTemp.Rows
                    dcTemp = New dc_DGMaster
                    moveDataRowToProperties(drTemp, dcTemp)
                    m_oc.Add(dcTemp)
                Next

            End If

        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    '
    '
    Public Function moveDataTableToXML() As XmlDocument

        Dim writer = New System.IO.StringWriter
        Dim m_dt_xml As Xml.XmlDocument = Nothing

        Try

            m_dt.WriteXml(writer, XmlWriteMode.WriteSchema, True)
            m_dt_xml = Nothing
            m_dt_xml = New Xml.XmlDocument
            m_dt_xml.LoadXml(writer.ToString)

        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        Finally

            writer = Nothing

        End Try

        Return m_dt_xml


    End Function



#End Region

#Region " Generic database operations "


    '
    '
    '
    Public Sub initDataSetAndTable()

        Try

            ' make sure the root stuff is there
            If m_ds Is Nothing Then
                m_ds = New DataSet
            Else
                m_ds = Nothing
                m_ds = New DataSet
            End If
            m_ds.SchemaSerializationMode = SchemaSerializationMode.IncludeSchema

            If m_dt Is Nothing Then
                m_dt = New DataTable
            Else
                m_dt = Nothing
                m_dt = New DataTable
            End If

            If m_oc Is Nothing Then
                m_oc = New Collection(Of dc_DGMaster)
            Else
                m_oc = Nothing
                m_oc = New Collection(Of dc_DGMaster)
            End If

            If m_dc Is Nothing Then
                m_dc = New dc_DGMaster(strUserId)
            Else
                m_dc = Nothing
                m_dc = New dc_DGMaster(strUserId)
            End If


            CreateTableColumns(m_dt)
            m_dt.TableName = DC_RackTables.tblDGMasters.ToString
            m_ds.Tables.Add(m_dt)

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try



    End Sub

    '
    '
    '
    Private Sub buildUpdateDgMastersCommand()

        Try
            m_updateCommand = m_db.GetStoredProcCommand("updateDgMasters")

            Dim cmdActive As DbCommand = m_updateCommand

            m_db.AddInParameter(cmdActive, "dgmTable", DbType.String, "dgmTable", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "dgmSurrogateMaster", DbType.String, "dgmSurrogateMaster", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "dgmSurrogateStencil", DbType.String, "dgmSurrogateStencil", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "dgmSurrogatePrefix", DbType.String, "dgmSurrogatePrefix", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "dgmMaster", DbType.String, "dgmMaster", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "dgmStencil", DbType.String, "dgmStencil", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "dgmFontValue", DbType.Int16, "dgmFontValue", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "dgmFontLabel", DbType.Int16, "dgmFontLabel", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "dgmCalloutWidth", DbType.Double, "dgmCalloutWidth", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "dgmComments", DbType.String, "dgmComments", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "dgmLastUpdate", DbType.Date, "dgmLastUpdate", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "dgmLastEdit", DbType.String, "dgmLastEdit", DataRowVersion.Current)


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    '
    '
    Private Sub buildInsertDgMastersCommand()

        Try
            m_insertCommand = m_db.GetStoredProcCommand("insertDgMasters")

            Dim cmdActive As DbCommand = m_insertCommand

            m_db.AddInParameter(cmdActive, "dgmTable", DbType.String, "dgmTable", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "dgmSurrogateMaster", DbType.String, "dgmSurrogateMaster", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "dgmSurrogateStencil", DbType.String, "dgmSurrogateStencil", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "dgmSurrogatePrefix", DbType.String, "dgmSurrogatePrefix", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "dgmMaster", DbType.String, "dgmMaster", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "dgmStencil", DbType.String, "dgmStencil", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "dgmFontValue", DbType.Int16, "dgmFontValue", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "dgmFontLabel", DbType.Int16, "dgmFontLabel", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "dgmCalloutWidth", DbType.Double, "dgmCalloutWidth", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "dgmComments", DbType.String, "dgmComments", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "dgmLastUpdate", DbType.Date, "dgmLastUpdate", DataRowVersion.Current)
            m_db.AddInParameter(cmdActive, "dgmLastEdit", DbType.String, "dgmLastEdit", DataRowVersion.Current)


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    '
    '
    Private Sub buildDeleteDgMastersCommand()

        Try
            m_deleteCommand = m_db.GetStoredProcCommand("deleteDgMasters")

            Dim cmdActive As DbCommand = m_deleteCommand
            m_db.AddInParameter(cmdActive, "dgmTable", DbType.String, "dgmTable", DataRowVersion.Current)

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    ' create a record based on property values move it to the generic datatable
    ' then copy the table to the database
    '
    Public Function addDgMasterDataSourceRecord _
            (ByVal tempDC As dc_DGMaster, _
             Optional ByVal blnSilent As Boolean = False) _
             As Integer

        Dim intRowsAffected As Integer = 0

        Try

            initDataSetAndTable()

            Dim m_dr As DataRow = m_ds.Tables(DC_RackTables.tblDGMasters.ToString).NewRow
            movePropertiesToDataRow(m_dr, tempDC)
            m_ds.Tables(DC_RackTables.tblDGMasters.ToString).Rows.Add(m_dr)
            intRowsAffected = dbUpdateDataSet()

            If blnSilent = False Then
                MsgBox("DataRowsUpated = " & intRowsAffected)
            End If

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

        Return intRowsAffected

    End Function

    '
    '
    '
    Public Function dbUpdateDataSet() As Int16

        Dim rowsAffected As Int16 = 0

        Try

            rowsAffected = m_db.UpdateDataSet(m_ds, DC_RackTables.tblDGMasters.ToString, _
                                        m_insertCommand, _
                                        m_updateCommand, _
                                        m_deleteCommand, _
                                        UpdateBehavior.Standard)
            'msgbox rowsAffected

        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If

        End Try

        Return rowsAffected

    End Function

#End Region

#Region " database queries "

    Public Sub dbSelectAllDGMasters()

        Try

            initDataSetAndTable()

            ' Retrieve the initial data
            Dim cmd As DbCommand = m_db.GetStoredProcCommand(readAll)
            m_db.LoadDataSet(cmd, m_ds, DC_RackTables.tblDGMasters.ToString)
            m_dt = m_ds.Tables(DC_RackTables.tblDGMasters.ToString)

            ' load the masters and stencil into the current 
            If blnLoadStencil = True Then
                preLoadDocumentStencil()
            End If


        Catch sql As SqlException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(sql, "Database Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

#End Region

#Region " Constructor "

    Public Sub New(Optional ByVal blnLoad As Boolean = True)
        Try


            m_db = DatabaseFactory.CreateDatabase()
            strUserId = Environment.UserDomainName & "/" & Environment.UserName
            '
            ' init an object class so that the default constants are available
            ' to the table definitions
            '
            If m_dc Is Nothing Then
                m_dc = New dc_DGMaster(strUserId)
            Else
                m_dc = Nothing
                m_dc = New dc_DGMaster(strUserId)
            End If

            initDataSetAndTable()

            m_blnLoadStencil = blnLoad

            _dictStencils = New Dictionary(Of Integer, String)
            _dictMasters = New Dictionary(Of Integer, String)


            _clsVisio = New DC_Rack.Support.clsVisioSupport

            If blnLoadStencil = True Then
                _StencilPath = My.Settings.path_custom_stencils
            Else
                _StencilPath = ""
            End If

            m_updateCommand = Nothing
            m_insertCommand = Nothing
            m_deleteCommand = Nothing

            buildInsertDgMastersCommand()
            buildDeleteDgMastersCommand()
            buildUpdateDgMastersCommand()


        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try




    End Sub


#End Region

#Region " Visio Interaction "


    '
    ' using the current product data table find the stencils and masters and load them
    ' into the current document stencil
    '
    Public Sub preLoadDocumentStencil()


        If _dictMasters Is Nothing Then
            _dictMasters = New Dictionary(Of Integer, String)
        Else
            _dictMasters.Clear()
        End If

        If _dictStencils Is Nothing Then
            _dictStencils = New Dictionary(Of Integer, String)
        Else
            _dictStencils.Clear()
        End If


        Try


            Dim strTable As String = ""
            Dim strSurrogateStencil As String = ""
            Dim strStencil As String = ""
            Dim strSurrogateMaster As String = ""
            Dim strMaster As String = ""

            If m_dt.Rows.Count = 0 Then dbSelectAllDGMasters()

            If m_dt.Rows.Count = 0 Then
                strStencil = ""
            End If

            Dim dgmQuery = From dgm In m_dt.AsEnumerable() _
                    Select dgm

            For Each p In dgmQuery
                strTable = p.Field(Of String)("dgmTable")

                ' these are the datagraphics
                strStencil = p.Field(Of String)("dgmStencil")
                If _dictStencils.ContainsValue(strStencil) = False Then
                    _dictStencils.Add(_dictStencils.Count + 1, strStencil)
                End If
                strMaster = p.Field(Of String)("dgmMaster")
                strMaster = "[" & strStencil & "," & strMaster & "]"
                If _dictMasters.ContainsValue(strMaster) = False Then
                    _dictMasters.Add(_dictMasters.Count + 1, strMaster)
                End If

                ' these are the surrogate shapes that we will apply them to
                strSurrogateStencil = p.Field(Of String)("dgmSurrogateStencil")
                If _dictStencils.ContainsValue(strStencil) = False Then
                    _dictStencils.Add(_dictStencils.Count + 1, strSurrogateStencil)
                End If
                strSurrogateMaster = p.Field(Of String)("dgmSurrogateMaster")
                strSurrogateMaster = "[" & strSurrogateStencil & "," & strSurrogateMaster & "]"
                If _dictMasters.ContainsValue(strSurrogateMaster) = False Then
                    _dictMasters.Add(_dictMasters.Count + 1, strSurrogateMaster)
                End If
            Next


            preLoadStencils(_dictStencils)
            preloadMasters(_dictMasters)



        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

        If _dictStencils IsNot Nothing Then
            _dictStencils = Nothing
        End If
        If _dictMasters IsNot Nothing Then
            _dictMasters = Nothing
        End If


    End Sub

    '
    ' go through the component data and load the master shapes into
    ' the document stencil. The collection passed in is csv strings
    ' (stencil name and master name)
    '
    Private Sub preloadMasters(ByVal dictMasters As Dictionary(Of Integer, String))

        Dim strMaster As String
        Dim strStencil As String
        Dim m_clsComp = New DC_Rack.Objects.clsComponents(drawingApplication, drawingDocument)

        Try

            Dim intX As Integer
            Dim vsoDocs As Microsoft.Office.Interop.Visio.Documents = drawingApplication.Documents
            Dim vsoStencil As Microsoft.Office.Interop.Visio.Document
            Dim vsoMasters As Microsoft.Office.Interop.Visio.Masters = drawingDocument.Masters
            Dim vsoMaster As Microsoft.Office.Interop.Visio.Master


            Dim colPair As Collection = New Collection
            For intX = 1 To dictMasters.Count
                Dim strMasterPair As String = dictMasters.Item(intX)
                colPair = Nothing
                colPair = New Collection
                colPair = BuildCsvCollection(strMasterPair)
                strStencil = colPair.Item(1)
                strMaster = colPair.Item(2)
                If strMaster <> "na" _
                    And strMaster <> "" Then
                    ' first check to see if the shape is in the
                    ' document stencil since it can error out, wrap in try-catch
                    Dim lclMaster As Microsoft.Office.Interop.Visio.Master = Nothing
                    Try
                        lclMaster = drawingDocument.Masters(strMaster)
                    Catch
                    End Try
                    If (lclMaster Is Nothing) Then
                        ' load the masters (the stencils were preloaded)
                        vsoStencil = vsoDocs.Item(strStencil)
                        vsoMaster = Nothing
                        ' make sure it is in the stencil
                        Try
                            vsoMaster = vsoStencil.Masters.Item(strMaster)
                        Catch
                        End Try
                        If vsoMaster IsNot Nothing Then
                            ' put the master into the document, not the page
                            lclMaster = drawingDocument.Drop(vsoMaster, 0, 0)

                            ' first fix any name issues
                            lclMaster.Name = strMaster
                            lclMaster.NameU = strMaster

                            ' clear any shape data definitions
                            Dim mstShape As Microsoft.Office.Interop.Visio.Shape
                            Dim intCt As Integer
                            Dim intY As Integer
                            ' make a copy of the master
                            Dim cpyMaster As Microsoft.Office.Interop.Visio.Master
                            cpyMaster = Nothing
                            cpyMaster = lclMaster.Open
                            mstShape = cpyMaster.Shapes(1)
                            intY = -1
                            intCt = -1
                            If mstShape.SectionExists(VisSectionIndices.visSectionProp, False) = True Then
                                intCt = mstShape.RowCount(VisSectionIndices.visSectionProp)
                                For intY = intCt To 0 Step -1
                                    If mstShape.CellsSRCExists(VisSectionIndices.visSectionProp, intY, VisCellIndices.visCustPropsType, VisExistsFlags.visExistsAnywhere) = True Then
                                        mstShape.DeleteRow(VisSectionIndices.visSectionProp, intY)
                                    End If
                                Next intY
                            End If

                            ' add our component shape data properties, not required for datagraphic shapes
                            ' m_clsComp.UpdateComponentProperties(mstShape, Nothing)

                            'put it back
                            cpyMaster.Close()
                        Else
                            Dim strError As String = "unable to find named master" & vbCrLf & strMasterPair
                            MsgBox(strError, MsgBoxStyle.Exclamation, "DGMaster unavailable")
                        End If
                    End If

                    ' end test for na or blank master name
                End If
HandleNext:
            Next intX

        Catch com As COMException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(com, "Com+ Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try


    End Sub

    '
    ' add a collection of stencil documents to the drawing
    ' pass in the active visio application, the stencil path, and a collection of stencil names
    '
    Private Sub preLoadStencils(ByVal dictStencils As Dictionary(Of Integer, String))

        Try

            Dim intX As Integer
            Dim blnLoaded As Boolean = False
            Dim blnFound As Boolean = False
            Dim strStencil As String = ""
            Dim strNew As String


            ' now load the rest of the stuff
            For intX = 1 To dictStencils.Count

                strStencil = dictStencils.Item(intX)
                blnLoaded = _clsVisio.TestStencilLoaded(drawingApplication, strStencil)

                If blnLoaded = False Then
                    strNew = _StencilPath & strStencil
                    ' since this is a preload, do not dock and open as hidden
                    blnFound = _clsVisio.visioOpenStencil(drawingApplication, strNew, VisOpenSaveArgs.visOpenHidden)
                    If blnFound = False Then
                        MsgBox("Failed to load " & strNew)
                    End If
                End If

            Next intX


        Catch com As COMException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(com, "Com+ Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub

    '
    ' based on the selected datarow and radiobutton, add a shape to the visio control
    ' and zoom in on it.
    '
    Public Sub dropMastersInDrawingAndZoom _
            (ByVal dcDgMaster As dc_DGMaster, _
             ByVal strMaster As String)

        Try

            Dim targetPage As Microsoft.Office.Interop.Visio.Page
            targetPage = DirectCast(drawingWindow.PageAsObj, Microsoft.Office.Interop.Visio.Page)

            Dim visShapes As Microsoft.Office.Interop.Visio.Shapes = targetPage.Shapes
            Dim visShape As Microsoft.Office.Interop.Visio.Shape
            If visShapes.Count > 0 Then
                For Each visShape In visShapes
                    visShape.Delete()
                Next
            End If

            Dim strMasterName As String = ""
            Select Case LCase(strMaster)
                Case "datagraphic"
                    strMasterName = dcDgMaster.dgmMaster
                Case "surrogate"
                    strMasterName = dcDgMaster.dgmSurrogateMaster
                Case Else
                    Exit Sub
            End Select


            ' first check to see if the shape is in the
            ' document stencil since it can error out, wrap in try-catch
            Dim networkMaster As Microsoft.Office.Interop.Visio.Master = Nothing
            Try
                networkMaster = drawingDocument.Masters(strMasterName)
            Catch
            End Try
            If (networkMaster IsNot Nothing) Then
                Dim strFound As String = ""

                Dim dropLocationX As Double = 0
                Dim dropLocationY As Double = 0

                ' Drop the master at the initial location. The end user
                ' can drag the shape around on the drawing page after it's
                ' dropped.


                Dim pageSheet As Microsoft.Office.Interop.Visio.Shape = targetPage.PageSheet

                ' put it in the width
                If pageSheet.CellExists("pagewidth", False) = True Then
                    dropLocationX = pageSheet.Cells("pagewidth").ResultIU / 2
                End If

                ' put it in the height
                If pageSheet.CellExists("pageheight", False) = True Then
                    dropLocationY = pageSheet.Cells("pageheight").ResultIU / 2
                End If

                visShape = targetPage.Drop(networkMaster, dropLocationX, dropLocationY)

                Dim dblLeft As Double = 0
                Dim dblTop As Double = 0
                Dim dblWidth As Double = 0
                Dim dblHeight As Double = 0
                Dim visCell As Microsoft.Office.Interop.Visio.Cell

                visCell = visShape.Cells("width")
                dblWidth = visCell.ResultIU

                visCell = visShape.Cells("height")
                dblHeight = visCell.ResultIU

                visCell = visShape.Cells("pinx")
                dblLeft = visCell.ResultIU - (dblWidth * 0.5)

                visCell = visShape.Cells("piny")
                dblTop = visCell.ResultIU + dblHeight + (dblHeight * 0.2)

                drawingWindow.SetViewRect(dblLeft, dblTop, dblWidth, dblHeight)
                drawingWindow.DeselectAll()

            Else



            End If


        Catch com As COMException
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(com, "Com+ Policy")
            If (rethrow) Then
                Throw
            End If
        Catch ex As Exception
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        End Try

    End Sub



    '
    ' create a csv string with end brackets from a collection
    '
    Private Function BuildCsvString _
        (ByVal colA As Collection) _
        As String

        Dim intY As Integer
        Dim strCsv As String = "[]"
        Try

            If (colA Is Nothing) _
                Or (colA.Count = 0) Then
                Exit Try
            Else
                With colA
                    ' start bracket
                    strCsv = "["
                    For intY = 1 To colA.Count
                        strCsv = strCsv & CStr(colA.Item(intY))
                        If intY <> colA.Count Then
                            strCsv = strCsv & ","
                        End If ' test for last position
                    Next intY
                    ' end bracket
                    strCsv = strCsv & "]"
                End With
            End If

        Catch ex As Exception
            '********* visio snippet vb.net visXXXX ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
            strCsv = "[]"
        End Try

        BuildCsvString = strCsv

    End Function


    '**************************************************************************
    '
    ' local string handlers
    '
    '**************************************************************************

    '
    '
    '
    Public Function memberOfCollection _
                (ByVal colObjects As Collection, _
                ByVal objMember As Object, _
                Optional ByVal strType As String = "string") _
                As Boolean

        Dim intX As Integer
        Dim blnReturn As Boolean = False
        Dim blnObject As Boolean = True

        Select Case strType
            Case "string"
                If Not (TypeOf objMember Is String) Then blnObject = False
                ' need to be explicit on com+ objects
            Case "int32"
                If Not (TypeOf objMember Is Int32) Then blnObject = False
            Case "double"
                If Not (TypeOf objMember Is Double) Then blnObject = False
            Case "integer"
                If Not (TypeOf objMember Is Integer) Then blnObject = False
                ' where long is 32bit on 32bit machine it becomes 64bit on upscale
            Case "long"
                If Not (TypeOf objMember Is Long) Then blnObject = False
            Case Else
                blnObject = False
        End Select

        Try
            ' we passed the objecttype test
            If blnObject = True Then
                For intX = 1 To colObjects.Count
                    Select Case strType
                        Case "string"
                            If LCase(objMember) = LCase(colObjects.Item(intX)) Then
                                blnReturn = True
                                Exit For
                            End If
                        Case "long", "double", "integer", "int32"
                            If objMember = colObjects.Item(intX) Then
                                blnReturn = True
                                Exit For
                            End If
                    End Select
                Next intX
            End If

            blnReturn = True


        Catch ex As Exception
            '********* visio snippet vb.net visXXXX ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        Finally
        End Try


        Return blnReturn

    End Function


    '
    ' pass in a bracketed string of csv separated items and pass back a collection
    ' strType allows the user to define what the collection is composed of
    '
    Public Function BuildCsvCollection _
            (ByVal strCsv As String, _
            Optional ByVal strType As String = "string", _
            Optional ByVal strDel As String = ",") _
            As Collection

        Dim colReturn As Collection
        colReturn = New Collection
        Try
            Dim intX As Integer

            ' get rid of first bracket
            If InStr(1, strCsv, "[") Then
                strCsv = Microsoft.VisualBasic.Replace(strCsv, "[", "")
            Else
                GoTo ExitHandler
            End If
            ' get rid of second bracket
            If InStr(1, strCsv, "]") Then
                strCsv = Microsoft.VisualBasic.Replace(strCsv, "]", "")
            Else
                GoTo ExitHandler
            End If

            intX = ParseItemsToCollection(strCsv, colReturn, strDel, strType)

ExitHandler:
            BuildCsvCollection = colReturn
            Exit Function

        Catch ex As Exception
            '********* visio snippet vb.net visXXXX ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
            BuildCsvCollection = colReturn
        End Try

    End Function

    '
    ' pass in a string, collection, and delimiter - update the collection and return count
    '
    Public Function ParseItemsToCollection _
        (ByVal s As String, _
        ByRef colA As Collection, _
        ByVal Delim As String, _
        Optional ByVal strType As String = "string") _
        As Integer

        Try

            '
            Dim p As Int32
            '
            ' Check for valid delimiter
            '
            If Delim = "" Then
                ParseItemsToCollection = -1
                Exit Function
            End If
            Dim intT32 As Int32

            '
            ' Copy Items
            '
            p = InStr(1, s, Delim, vbTextCompare)
            Do While p > 0

                Select Case strType
                    Case "string"
                        colA.Add(Left$(s, p - 1))
                    Case "integer"
                        colA.Add(CInt(Left$(s, p - 1)))
                    Case "double"
                        colA.Add(CDbl(Left$(s, p - 1)))
                    Case "int32"
                        intT32 = CInt(Left$(s, p - 1))
                        colA.Add(intT32)
                    Case "long"
                        colA.Add(CLng(Left$(s, p - 1)))
                End Select
                s = Mid$(s, p + 1)
                p = InStr(1, s, Delim, vbTextCompare)
            Loop
            '
            ' Copy Last Item
            '
            Select Case strType
                Case "string"
                    colA.Add(s)
                Case "integer"
                    colA.Add(CInt(s))
                Case "double"
                    colA.Add(CDbl(s))
                Case "int32"
                    intT32 = CInt(s)
                    colA.Add(intT32)
                Case "long"
                    colA.Add(CLng(s))
            End Select


        Catch ex As Exception
            '********* visio snippet vb.net visXXXX ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If
        Finally

            ParseItemsToCollection = colA.Count
        End Try

    End Function




#End Region

#Region " Data Graphics "





    '********* visio snippet vb.net vis0029vb ******************

    Public Function GetSourceData _
        (ByVal shp As Microsoft.Office.Interop.Visio.Shape, _
        ByVal allCells As Boolean) _
        As Object

        Dim avarFormulaArray As Object = Nothing
        Dim iRow As Integer = 0
        Dim iCellPerRow As Integer = 0
        Dim iCell As Integer = 0


        Try

            Dim iRows As Integer
            iRows = shp.RowCount(VisSectionIndices.visSectionProp)
            If iRows = 0 Then
                GetSourceData = Nothing
                Exit Function
            End If

            Dim iCellsPerRow As Integer
            If allCells Then
                iCellsPerRow = 11
            Else
                iCellsPerRow = 3
            End If

            ReDim avarFormulaArray(0 To ((iRows) * iCellsPerRow))


            For iRow = 0 To iRows - 1
                For iCellPerRow = 0 To iCellsPerRow - 1
                    iCell = (iRow * iCellsPerRow)
                    avarFormulaArray(iCell) = shp.CellsSRC(VisSectionIndices.visSectionProp, iRow, VisCellIndices.visCustPropsValue).RowNameU
                    avarFormulaArray(iCell + 1) = shp.CellsSRC(VisSectionIndices.visSectionProp, iRow, VisCellIndices.visCustPropsLabel).FormulaU
                    avarFormulaArray(iCell + 2) = shp.CellsSRC(VisSectionIndices.visSectionProp, iRow, VisCellIndices.visCustPropsValue).FormulaU

                    If allCells Then
                        avarFormulaArray(iCell + 3) = shp.CellsSRC(VisSectionIndices.visSectionProp, iRow, VisCellIndices.visCustPropsPrompt).FormulaU
                        avarFormulaArray(iCell + 4) = shp.CellsSRC(VisSectionIndices.visSectionProp, iRow, VisCellIndices.visCustPropsType).FormulaU
                        avarFormulaArray(iCell + 5) = shp.CellsSRC(VisSectionIndices.visSectionProp, iRow, VisCellIndices.visCustPropsFormat).FormulaU

                        avarFormulaArray(iCell + 6) = shp.CellsSRC(VisSectionIndices.visSectionProp, iRow, VisCellIndices.visCustPropsSortKey).FormulaU
                        avarFormulaArray(iCell + 7) = shp.CellsSRC(VisSectionIndices.visSectionProp, iRow, VisCellIndices.visCustPropsInvis).FormulaU
                        avarFormulaArray(iCell + 8) = shp.CellsSRC(VisSectionIndices.visSectionProp, iRow, VisCellIndices.visCustPropsAsk).FormulaU

                        avarFormulaArray(iCell + 9) = shp.CellsSRC(VisSectionIndices.visSectionProp, iRow, VisCellIndices.visCustPropsLangID).FormulaU
                        avarFormulaArray(iCell + 10) = shp.CellsSRC(VisSectionIndices.visSectionProp, iRow, VisCellIndices.visCustPropsCalendar).FormulaU
                    End If
                Next iCellPerRow
            Next iRow


        Catch com As COMException
            ' insert com exception handler here
            '********* visio snippet vb.net vis0056vb ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(com, "Com+ Policy")
            If (rethrow) Then
                Throw
            End If

        Catch ex As Exception
            ' insert exception handler here
            '********* visio snippet vb.net vis0059vb ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If

        End Try

        Return avarFormulaArray


    End Function





    '********* visio snippet vb.net vis0035vb ******************

    Public Function SetTargetData _
            (ByVal shp As Microsoft.Office.Interop.Visio.Shape, _
            ByVal allCells As Boolean, _
            ByVal forceAdd As Boolean, _
            ByVal matchByName As Boolean, _
            ByVal matchByLabel As Boolean, _
            ByVal aryData As Object) _
            As Boolean

        ' Set the data on source shape, optionally copying all cells,
        ' forcing an add, matching by row name or label

        Dim iRowTarget As Integer = 0
        Dim iRow As Integer = 0
        Dim iRows As Integer = 0
        Dim rowName As String = 0
        Dim rowLabel As String = 0
        Dim iRowTest As Integer = 0
        Dim iCellsPerRow As Integer = 0
        Dim totalCells As Integer = 0
        Dim blnResponse As Boolean = False

        Try

            If allCells Then
                iCellsPerRow = 11
            Else
                iCellsPerRow = 3
            End If

            totalCells = UBound(aryData)
            If totalCells = 0 Then
                GoTo ExitFunction
            End If

            iRows = shp.RowCount(VisSectionIndices.visSectionProp)
            If shp.SectionExists(VisSectionIndices.visSectionProp, Microsoft.Office.Interop.Visio.VisExistsFlags.visExistsAnywhere) = False Then
                shp.AddSection(VisSectionIndices.visSectionProp)
            End If


            For iRow = 0 To totalCells - 1 Step iCellsPerRow
                iRowTarget = -1
                rowName = aryData(iRow)
                rowLabel = aryData(iRow + 1)
                If matchByName Then
                    'Firstly, test if row name exists
                    If Not shp.CellExistsU("Prop." & rowName, VisExistsFlags.visExistsAnywhere) = 0 Then
                        iRowTarget = shp.CellsU("Prop." & rowName).Row
                    End If
                End If

                If iRowTarget < 0 And matchByLabel Then
                    'Secondly, test if label exists
                    For iRowTest = 0 To shp.RowCount(VisSectionIndices.visSectionProp) - 1
                        If UCase(shp.CellsSRC(VisSectionIndices.visSectionProp, iRowTest, VisCellIndices.visCustPropsLabel).ResultStr("")) = UCase(rowLabel) Then
                            iRowTarget = iRowTest
                            Exit For
                        End If
                    Next iRowTest
                End If

                If forceAdd And iRowTarget < 0 Then
                    If matchByName Then
                        iRowTarget = shp.AddNamedRow(VisSectionIndices.visSectionProp, rowName, 0)
                    Else
                        iRowTarget = shp.AddRow(VisSectionIndices.visSectionProp, shp.RowCount(VisSectionIndices.visSectionProp), 0)
                    End If
                End If

                If iRowTarget > -1 Then

                    setCellFormula(shp, Microsoft.Office.Interop.Visio.VisSectionIndices.visSectionProp, iRowTarget, Microsoft.Office.Interop.Visio.VisCellIndices.visCustPropsLabel, aryData(iRow + 1))
                    setCellFormula(shp, Microsoft.Office.Interop.Visio.VisSectionIndices.visSectionProp, iRowTarget, Microsoft.Office.Interop.Visio.VisCellIndices.visCustPropsValue, aryData(iRow + 2))
                    If allCells Then
                        setCellFormula(shp, Microsoft.Office.Interop.Visio.VisSectionIndices.visSectionProp, iRowTarget, Microsoft.Office.Interop.Visio.VisCellIndices.visCustPropsPrompt, aryData(iRow + 3))
                        setCellFormula(shp, Microsoft.Office.Interop.Visio.VisSectionIndices.visSectionProp, iRowTarget, Microsoft.Office.Interop.Visio.VisCellIndices.visCustPropsType, aryData(iRow + 4))
                        setCellFormula(shp, Microsoft.Office.Interop.Visio.VisSectionIndices.visSectionProp, iRowTarget, Microsoft.Office.Interop.Visio.VisCellIndices.visCustPropsFormat, aryData(iRow + 5))

                        setCellFormula(shp, Microsoft.Office.Interop.Visio.VisSectionIndices.visSectionProp, iRowTarget, Microsoft.Office.Interop.Visio.VisCellIndices.visCustPropsSortKey, aryData(iRow + 6))
                        setCellFormula(shp, Microsoft.Office.Interop.Visio.VisSectionIndices.visSectionProp, iRowTarget, Microsoft.Office.Interop.Visio.VisCellIndices.visCustPropsInvis, aryData(iRow + 7))
                        setCellFormula(shp, Microsoft.Office.Interop.Visio.VisSectionIndices.visSectionProp, iRowTarget, Microsoft.Office.Interop.Visio.VisCellIndices.visCustPropsAsk, aryData(iRow + 8))

                        setCellFormula(shp, Microsoft.Office.Interop.Visio.VisSectionIndices.visSectionProp, iRowTarget, Microsoft.Office.Interop.Visio.VisCellIndices.visCustPropsLangID, aryData(iRow + 9))
                        setCellFormula(shp, Microsoft.Office.Interop.Visio.VisSectionIndices.visSectionProp, iRowTarget, Microsoft.Office.Interop.Visio.VisCellIndices.visCustPropsCalendar, aryData(iRow + 10))
                    End If
                End If
            Next iRow

            blnResponse = True

ExitFunction:

        Catch com As COMException
            ' insert com exception handler here

            blnResponse = False
        Catch err As Exception
            ' insert exception handler here

            blnResponse = False
        End Try

        Return blnResponse

    End Function




    '********* visio snippet vb.net vis0005vb ******************

    Private Sub setCellFormula _
        (ByVal shp As Microsoft.Office.Interop.Visio.Shape, _
        ByVal iSect As Integer, _
        ByVal iRow As Integer, _
        ByVal iCell As Integer, _
        ByVal formula As String)

        Try


            ' test to make sure cell exists first (aee)
            If shp.CellsSRCExists(iSect, iRow, iCell, False) = True Then
                'Transfer cell formula if different
                If Not shp.CellsSRC(iSect, iRow, iCell).FormulaU = formula Then
                    shp.CellsSRC(iSect, iRow, iCell).FormulaForceU = formula
                End If
            End If

        Catch com As COMException
            ' add com exception handler here
            '********* visio snippet vb.net vis0056vb ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(com, "Com+ Policy")
            If (rethrow) Then
                Throw
            End If

        Catch ex As Exception
            ' add exception handler here
            '********* visio snippet vb.net vis0059vb ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If




        End Try


    End Sub





    '********* visio snippet vb.net vis0015vb ******************
    Public Sub AddDataGraphicToShape _
        (ByVal visShape As Microsoft.Office.Interop.Visio.Shape, _
        ByVal strDgMaster As String)

        Dim visMaster As Microsoft.Office.Interop.Visio.Master
        Dim visDG As Microsoft.Office.Interop.Visio.Master = Nothing

        Try

            'Check that the requested Data Graphic Master exits
            For Each visMaster In visShape.Application.ActiveDocument.Masters
                If visMaster.Type = VisMasterTypes.visTypeDataGraphic Then
                    If visMaster.Name = strDgMaster Then
                        visDG = visMaster
                        Exit For
                    End If
                End If
            Next

            If visDG Is Nothing Then
                MsgBox("Unable to find the Data Graphic Master : " & strDgMaster)
                Exit Sub
            End If

            'Apply the Data Graphic to the Shape
            visShape.DataGraphic = visDG

            ' unlock the groups so we can change the font sizes
            _clsVisio.recurseApplyCellFormula(visShape, VisSectionIndices.visSectionObject, VisRowIndices.visRowLock, VisCellIndices.visLockGroup, "0")

            If visShape.CellExists("user.dbIntLabelFont", False) = True Then

                Dim intLabelFont As Int16 = CInt(visShape.Cells("user.dbintlabelfont").ResultIU)
                Dim intValueFont As Int16 = CInt(visShape.Cells("user.dbintvaluefont").ResultIU)
                Dim dblCallout As Double = CDbl(visShape.Cells("user.dbdblcallout").ResultIU)

                ' set the callout width = 85% of the surrogate shapes width (as a formula)
                Dim strId As String = visShape.NameID & "!Width*.85"
                strId = "Guard(" & strId & ")"

                _clsVisio.recurseApplyCustomFormula(visShape, "prop.msvcalloutproplabelsize", intLabelFont.ToString & " pt")
                _clsVisio.recurseApplyCustomFormula(visShape, "prop.msvcalloutpropvaluesize", intValueFont.ToString & " pt")
                _clsVisio.recurseApplyCustomFormula(visShape, "prop.msvcalloutpropwidth", strId)

            End If

        Catch com As COMException
            ' insert com exception handler here
            '********* visio snippet vb.net vis0056vb ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(com, "Com+ Policy")
            If (rethrow) Then
                Throw
            End If

        Catch ex As Exception
            ' insert exception handler here
            '********* visio snippet vb.net vis0059vb ******************
            Dim rethrow As Boolean = ExceptionPolicy.HandleException(ex, "Log Only Policy")
            If (rethrow) Then
                Throw
            End If

        End Try


    End Sub





#End Region





End Class
